package user

import (
	"fmt"
	"gitee.com/binny_w/gin_starter/model/rsa"
	"gitee.com/binny_w/go-util"
	util2 "gitee.com/binny_w/go-util/v2"
	"github.com/gin-gonic/gin"
	"time"
)

func insertFixer(c *gin.Context, row *util2.MysqlRow) error {
	if row.IsEmpty() {
		return fmt.Errorf("数据为空")
	}
	pass := row.ToStr("pasd")
	if pass == "" {
		return fmt.Errorf("密码未设置")
	}
	var err error
	if pass, err = rsa.Decode(pass); err != nil {
		return err
	} else if len(pass) < 6 || len(pass) > 20 {
		return ErrWrongPassSize
	} else if pass, err = util.StrHashPass(pass); err != nil {
		return err
	}
	row.Set("pasd", pass)
	row.Set("created_at", time.Now().Unix())
	if role := row.ToStr("role"); role != "" {
		c.Set("role", role)
	}
	return nil
}
